/*
 * Copyright 2007 Yegor Jbanov
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.spicesoft.clientobjects.samples.list;

import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.beans.BeanAdapter;
import com.jgoodies.binding.list.SelectionInList;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.spicesoft.clientobjects.swing.SimpleListModel;

public class CountryListForm extends JFrame {

  private CountryListState stateBean;

  private CellConstraints cc = new CellConstraints();

  private JTextField nameField;

  private JList countryList;

  private JButton addButton;

  private JButton updateButton;

  private JButton deleteButton;

  private JButton closeButton;

  public CountryListForm(CountryListState stateBean) {
    this.stateBean = stateBean;
    initUI();
  }

  private void initUI() {
    this.setTitle("Country List Editor");

    // Define the layout
    JPanel cp = new JPanel();
    FormLayout layout = new FormLayout("5dlu, p, 5dlu, f:p:g, 5dlu",
        "3dlu, p, 3dlu, f:50dlu:g, 3dlu, p, 3dlu");
    cp.setLayout(layout);
    this.setContentPane(cp);

    // Create property adapters using JGoodies API
    BeanAdapter adapter = new BeanAdapter(this.stateBean, true);
    ValueModel countryNameHolder = adapter.getValueModel("countryName");
    ValueModel selectedCountryHolder = adapter.getValueModel("selectedCountry");
    ValueModel indexHolder = adapter.getValueModel("selectedIndex");

    // When selection in the list changes, pass the selected country to the
    // country name field
    selectedCountryHolder.addValueChangeListener(new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        stateBean.setCountryName(stateBean.getSelectedCountry());
      }
    });

    // Create and add UI elements
    nameField = BasicComponentFactory.createTextField(countryNameHolder);
    cp.add(new JLabel("Country:"), cc.xy(2, 2));
    cp.add(nameField, cc.xy(4, 2));

    countryList = BasicComponentFactory
        .createList(new SelectionInList(new SimpleListModel(stateBean.getCountries()),
            selectedCountryHolder, indexHolder));

    cp.add(new JScrollPane(countryList), cc.xyw(2, 4, 3));

    JPanel buttonPanel = createButtonPanel();
    cp.add(buttonPanel, cc.xyw(2, 6, 3));
  }

  private JPanel createButtonPanel() {
    // Define the layout
    JPanel buttonPanel = new JPanel();
    FormLayout layout = new FormLayout("0dlu:g, f:p, 5dlu, f:p, 5dlu, f:p, 5dlu, f:p",
        "p");
    buttonPanel.setLayout(layout);

    // Create and add buttons
    deleteButton = new JButton("Delete");
    addButton = new JButton("Add");
    updateButton = new JButton("Update");
    closeButton = new JButton("Close");

    buttonPanel.add(deleteButton, cc.xy(2, 1));
    buttonPanel.add(addButton, cc.xy(4, 1));
    buttonPanel.add(updateButton, cc.xy(6, 1));
    buttonPanel.add(closeButton, cc.xy(8, 1));

    return buttonPanel;
  }

  public void addDeleteListener(ActionListener addListener) {
    this.deleteButton.addActionListener(addListener);
  }

  public void addAddListener(ActionListener addListener) {
    this.addButton.addActionListener(addListener);
  }

  public void addUpdateListener(ActionListener updateListener) {
    this.updateButton.addActionListener(updateListener);
  }

  public void addCloseListener(ActionListener closeListener) {
    this.closeButton.addActionListener(closeListener);
  }

}
